
#include "atomic_op_namespace.h"

#include <sys/atomic.h>

uint16_t fetch_and_nand_2(volatile uint16_t *, uint16_t, ...)
    asm("__sync_fetch_and_nand_2");
uint16_t nand_and_fetch_2(volatile uint16_t *, uint16_t, ...)
    asm("__sync_nand_and_fetch_2");

uint16_t
fetch_and_nand_2(volatile uint16_t *addr, uint16_t val, ...)
{
	uint16_t old, new;

	do {
		old = *addr;
		new = ~(old & val);
	} while (atomic_cas_16(addr, old, new) != old);
	return old;
}

uint16_t
nand_and_fetch_2(volatile uint16_t *addr, uint16_t val, ...)
{
	uint16_t old, new;

	do {
		old = *addr;
		new = ~(old & val);
	} while (atomic_cas_16(addr, old, new) != old);
	return new;
}
